# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=collectd
pkgver=5.9.2
pkgrel=0
pkgdesc="The system statistics collection daemon"
options="!check" # test_common fails
url="https://collectd.org"
arch="all"
license="GPL-2.0-or-later"
pkgusers="collectd"
pkggroups="collectd"
makedepends="curl-dev net-snmp-dev postgresql-dev perl-dev python3-dev
	libgcrypt-dev mariadb-connector-c-dev zlib-dev iptables-dev
	rrdtool-dev yajl-dev libvirt-dev lm-sensors-dev libxml2-dev
	hiredis-dev eudev-dev mosquitto-dev lua-dev lvm2-dev openipmi-dev
	libpcap-dev liboping-dev
	autoconf automake libtool"
subpackages="$pkgname-dev $pkgname-doc $pkgname-libs
	$pkgname-perl $pkgname-snmp $pkgname-curl:_curl $pkgname-utils
	$pkgname-write_http $pkgname-nginx $pkgname-apache $pkgname-postgresql
	$pkgname-network $pkgname-mysql $pkgname-rrdtool $pkgname-sensors
	$pkgname-python:_python $pkgname-iptables:_ipt $pkgname-bind
	$pkgname-ascent $pkgname-virt $pkgname-redis $pkgname-write_redis
	$pkgname-log_logstash $pkgname-ceph $pkgname-disk $pkgname-lvm
	$pkgname-lua $pkgname-mqtt $pkgname-ipmi $pkgname-ping $pkgname-dns"
install="$pkgname.pre-install"
source="https://github.com/collectd/collectd/releases/download/collectd-$pkgver/collectd-$pkgver.tar.bz2
	collectd.initd
	gnu_source.patch
	"


# security fixes:
#   5.5.2-r0:
#   -  CVE-2016-6254

prepare() {
	default_prepare

	autoreconf -vif
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc/collectd \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstate=/var \
		--with-libiptc \
		--enable-rrdtool \
		--enable-bind \
		--disable-werror \
		--with-perl-bindings=INSTALLDIRS=vendor

	make

	# disable plugins by default since its in a subpackage
	sed -i -e 's/^LoadPlugin network/#LoadPlugin network/' \
		src/collectd.conf
	sed -i -e 's/^LoadPlugin rrdtool/#LoadPlugin rrdtool/' \
		src/collectd.conf
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install
	chown root:collectd "$pkgdir"/etc/collectd/collectd.conf
	find "$pkgdir" \( -name perllocal.pod -o -name .packlist \) -delete
	install -m755 -D "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
}

perl() {
	pkgdesc="perl bindings to collectd"
	mkdir -p "$subpkgdir"/usr/lib/collectd \
		"$subpkgdir"/usr/share
	mv "$pkgdir"/usr/lib/collectd/perl.so \
		"$subpkgdir"/usr/lib/collectd/
	mv "$pkgdir"/usr/lib/perl* "$subpkgdir"/usr/lib/
	mv "$pkgdir"/usr/share/perl* "$subpkgdir"/usr/share/
}

utils() {
	pkgdesc="$pkgname utilities"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/* "$subpkgdir"/usr/bin/
}

_plugin() {
	local mod=$1
	local desc=${2:-$mod}
	pkgdesc="$desc pluin for collectd"
	depends="collectd"
	install -d "$subpkgdir"/usr/lib/collectd
	mv "$pkgdir"/usr/lib/collectd/$mod.so "$subpkgdir"/usr/lib/collectd/
}

snmp()		{ _plugin snmp; }
_curl()		{ _plugin 'curl*'; }
write_http()	{ _plugin write_http; }
nginx()		{ _plugin nginx; }
apache()	{ _plugin apache; }
postgresql()	{ _plugin postgresql; }
mysql()		{ _plugin mysql; }
network()	{ _plugin network; }
rrdtool()	{ _plugin 'rrd*'; }
sensors()	{ _plugin sensors; }
_python()	{ _plugin python; }
_ipt()		{ _plugin iptables; }
bind()		{ _plugin bind; }
ascent()	{ _plugin ascent; }
redis()		{ _plugin redis; }
write_redis()	{ _plugin write_redis; }
log_logstash()	{ _plugin log_logstash; }
ceph()		{ _plugin ceph; }
disk()		{ _plugin disk; }
lvm()		{ _plugin lvm; }
lua()		{ _plugin lua; }
mqtt()		{ _plugin mqtt; }
virt()		{ _plugin virt; provides="$pkgname-libvirt"; }
ipmi()		{ _plugin ipmi; }
ping()		{ _plugin ping; }
dns()		{ _plugin dns; }

sha512sums="5386e4d0b9c350a0719924bf440f53bf3cdedefefe6bc7b154501dfdf7d2d98fa5dd31341cbed41023f7a129f68bda694e9d06f2da69ea2e61372d1b237b64a7  collectd-5.9.2.tar.bz2
a286c2164af196154d017658d6e5aa3cc4f76dadf90b68c3021418979d9f0377776bb990d737ae5c916c04475725365aea8b78e3bc5e2f3679d5a99f8b9af13e  collectd.initd
3078e43fc1aa5e5d5f7042472f465cc3b077c59f43a9867ade038e59bb210f26f53bb9a1e1e8a198ca53939a4b2e03a1f05628aa54f826746f970faa36acff6a  gnu_source.patch"
